//
//  Consume_SelectInstalmentView.m
//  JinFuThree
//
//  Created by 爱尚 on 2018/9/4.
//  Copyright © 2018年 毛韶谦. All rights reserved.
//

#import "Consume_SelectInstalmentView.h"

#define Tag_RecentBase    20000 //最近搜索按钮基准值
#define LeftInterSpace    25 //左边间隙

@interface Consume_SelectInstalmentView()

@property(nonatomic,strong)UIButton *cancelBtn;
@property(nonatomic,strong)UIButton *sureBtn;
@property(nonatomic,strong)UIView *selectView;
@property(nonatomic,strong)UILabel *detailLabel;
@property(nonatomic,strong)UIView *grayView;
@property(nonatomic,strong)UIView *whiteView;



@property(nonatomic,strong)UIView *InstallmentPeriodsView;

@end


@implementation Consume_SelectInstalmentView

-(instancetype)init{
    self = [super init];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        self.frame = CGRectMake(0, 0, ScreenWidthMao, ScreenHeight);
        [self initialize];
        
    }
    return self;
}

-(void)show:(NSArray *)selectArr{
    [self installmentPeriodsBtn:selectArr.count view:self.selectView array:selectArr];
    
    NSString *str =selectArr[0];
    NSString *strr = [NSString stringWithFormat:@"¥458.25x%@",str];
    NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc]initWithString:strr];
    [attrStr addAttribute:NSFontAttributeName value:
     [UIFont systemFontOfSize:14.0f] range:NSMakeRange(0, 1)];
    [attrStr addAttribute:NSFontAttributeName value:
     [UIFont systemFontOfSize:27.0f] range:NSMakeRange(1, strr.length-2-str.length)];
    self.detailLabel.attributedText = attrStr;
    [[UIApplication sharedApplication].keyWindow.rootViewController.view addSubview:self];
}

-(void)sureBtnAction:(UIButton *)sender{
    for (int i=0; i<self.InstallmentArray.count; i++) {
         UIButton *btn = self.InstallmentArray[i];
        if (btn.selected == YES) {
            sender.tag = btn.tag;
            
            if ([self.selectInstalmentViewDelegate respondsToSelector:@selector(sureBtnAction:)]) {
                [self.selectInstalmentViewDelegate sureBtnAction:sender];
            }
            [self cancelBtnAction];
        }
    }
    
    
}

-(void)cancelBtnAction{
    
    for (UIView *tempView in [UIApplication sharedApplication].keyWindow.rootViewController.view.subviews) {
        if ([tempView isMemberOfClass: [Consume_SelectInstalmentView class]]) {
            [tempView removeFromSuperview];
        }
    }
}

-(void)initialize{
    
    self.backgroundColor = [UIColor clearColor];
    _grayView = [[UIView alloc]init];
    _grayView.backgroundColor = [UIColor blackColor];
    _grayView.alpha = 0.3;
    [self addSubview:_grayView];
    [_grayView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.mas_equalTo(self);
    }];
    
    _whiteView = [[UIView alloc]init];
    _whiteView.backgroundColor = [UIColor whiteColor];
    [self addSubview:_whiteView];
    [_whiteView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(self);
        make.height.mas_offset(@350);
        
    }];
    
    _cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [_cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
    [_cancelBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    _cancelBtn.titleLabel.font = UIFontMaoNormal;
    [_cancelBtn addTarget:self action:@selector(cancelBtnAction) forControlEvents:UIControlEventTouchUpInside];
    [self.whiteView addSubview:_cancelBtn];
    [_cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.whiteView.mas_left).offset(20);
        make.top.mas_equalTo(self.whiteView.mas_top).offset(20);
        make.height.mas_offset(@16);
    }];
    
    _sureBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [_sureBtn setTitle:@"确定" forState:UIControlStateNormal];
    [_sureBtn setTitleColor:ThemeMaoColor forState:UIControlStateNormal];
    _sureBtn.titleLabel.font = UIFontMaoNormal;
    [_sureBtn addTarget:self action:@selector(sureBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.whiteView addSubview:_sureBtn];
    [_sureBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.whiteView.mas_right).offset(-20);
        make.centerY.height.mas_equalTo(self.cancelBtn);
    }];
    
    UIView *line1 = [[UIView alloc]init];
    line1.backgroundColor = ThemeMaoLineColor;
    [self.whiteView addSubview:line1];
    [line1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self.whiteView);
        make.height.mas_offset(@0.5);
        make.top.mas_equalTo(self.cancelBtn.mas_bottom).offset(20);
    }];
    
    _detailLabel = [[UILabel alloc]init];
    _detailLabel.text = @"¥458.25";
    
    NSString *str = @"¥458.25";
    NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc]initWithString:str];
    [attrStr addAttribute:NSFontAttributeName value:
     [UIFont systemFontOfSize:14.0f] range:NSMakeRange(0, 1)];  //字体大小为20.0f
    [attrStr addAttribute:NSFontAttributeName value:
     [UIFont systemFontOfSize:27.0f] range:NSMakeRange(1, str.length-1)];
    _detailLabel.attributedText = attrStr;
    _detailLabel.textAlignment = NSTextAlignmentCenter;
    _detailLabel.textColor = HexColor(0x333333);
    [self.whiteView addSubview:_detailLabel];
    [self.detailLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(line1.mas_bottom).offset(22);
        make.centerX.width.mas_equalTo(self.whiteView);
        make.height.mas_offset(@38);
    }];
    
    UIView *line2 = [[UIView alloc]init];
    line2.backgroundColor = ThemeMaoLineColor;
    [self.whiteView addSubview:line2];
    [line2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self.whiteView);
        make.height.mas_offset(@0.5);
        make.top.mas_equalTo(self.detailLabel.mas_bottom).offset(22);
    }];
    
    UILabel *titleLabel = [[UILabel alloc]init];
    titleLabel.text = @"分期期数";
    titleLabel.textColor = HexColor(0x333333);
    titleLabel.font = [UIFont systemFontOfSize:15];
    [self.whiteView addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.whiteView.mas_left).offset(25);
        make.top.mas_equalTo(line2.mas_bottom).offset(22);
        make.height.mas_offset(@16);
    }];
    
    _selectView = [[UIView alloc]init];
    _selectView.backgroundColor = [UIColor whiteColor];
    [self.whiteView addSubview:_selectView];
    [_selectView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(self.whiteView);
        make.top.mas_equalTo(titleLabel.mas_bottom).offset(22);
    }];
    
    
    
    
}

#pragma mark ---------根据数据布局分期期数按钮页面---------------
-(void)installmentPeriodsBtn:(NSInteger)count view:(UIView *)view array:(NSArray *)array{
    
    CGFloat cruuentY = 0;
    CGFloat cruuentX = LeftInterSpace;
    CGFloat forwardHeight = 0;
    for (int i = 0; i<count; i++) {
        UIButton *btton = [UIButton buttonWithType:UIButtonTypeCustom];
        btton.backgroundColor = [UIColor clearColor];
        btton.tag = i+100;
        CGSize btnSize = CGSizeMake((ScreenWidthMao-12*3-50)/4, ((ScreenWidthMao-12*3-50)/4)/144*64);
        [btton setTitle:array[i] forState:UIControlStateNormal];
        [btton setTitleColor:HexColor(0x333333) forState:UIControlStateNormal];
        [btton setTitleColor:HexColor(0xFFFFFF) forState:UIControlStateSelected];
        btton.titleLabel.font = UIFontMaoSmall;
//        [btton setBackgroundImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
        [btton setBackgroundImage:[UIImage imageNamed:@"select_btn"] forState:UIControlStateSelected];
        btton.layer.masksToBounds = YES;
        btton.layer.cornerRadius = 2;
        btton.layer.borderColor = HexColor(0x999999).CGColor;
        btton.layer.borderWidth = 0.5;
        if (cruuentX + btnSize.width >ScreenWidthMao ) {
            cruuentX = LeftInterSpace;
            cruuentY = cruuentY + forwardHeight + 25;
        }
        btton.frame = CGRectMake(cruuentX, cruuentY, btnSize.width, ((ScreenWidthMao-12*3-50)/4)/144*64);
        [btton addTarget:self action:@selector(installmentPeriodsBtnAction:) forControlEvents:UIControlEventTouchUpInside];
        [view addSubview:btton];
        forwardHeight = 25;
        cruuentX = cruuentX + btnSize.width + 15;
        if (i == 0) {
            btton.selected = YES;
        }
        [self.InstallmentArray addObject:btton];
        
        
    }
}

-(void)installmentPeriodsBtnAction:(UIButton *)sender{
    for (int i=0; i<self.InstallmentArray.count; i++) {
        UIButton *btn = self.InstallmentArray[i];
        if (btn.tag == sender.tag) {
            btn.selected = YES;
            NSString *str =btn.titleLabel.text;
            NSString *strr = [NSString stringWithFormat:@"¥458.25x%@",str];
            NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc]initWithString:strr];
            [attrStr addAttribute:NSFontAttributeName value:
            [UIFont systemFontOfSize:14.0f] range:NSMakeRange(0, 1)];
            [attrStr addAttribute:NSFontAttributeName value:
             [UIFont systemFontOfSize:27.0f] range:NSMakeRange(1, strr.length-2-str.length)];
            self.detailLabel.attributedText = attrStr;
        }else{
            btn.selected = NO;
        }
    }
}

-(NSMutableArray *)InstallmentArray{
    if (!_InstallmentArray) {
        _InstallmentArray = [[NSMutableArray alloc]init];
    }
    return _InstallmentArray;
}

@end
